package Y2024.M12.D12;

import sun.misc.ConditionLock;
import sun.misc.Lock;

import java.util.concurrent.locks.Condition;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 25228
 * Date: 2024-12-12
 * Time: 8:47
 */
public class Main {
    public static void main(String[] args) {

    }
}
class TicketPool {
    static int n=0;
    Lock lock= new ConditionLock();
    //放入一张
    void put() throws InterruptedException {
        lock.lock();
        n++;
        Thread.sleep(1000);
        lock.unlock();
    }
    //抢走一张
    void get() throws InterruptedException {
        lock.lock();
        while (n==0){

        }
        n--;
        lock.unlock();
    }
}
//用户
class Thr1 implements Runnable{
    int ID;
    Thr1(int id){
        this.ID=id;
    }
    @Override
    public void run() {
        TicketPool ticketPool=new TicketPool();
        try {
            ticketPool.get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(ID+"抢到了票");
    }
}
//商家
class Thr2 implements Runnable{

    @Override
    public void run() {
        TicketPool ticketPool=new TicketPool();
        for (int i = 0; i < 20; i++) {
            try {
                ticketPool.put();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}